package com.lxx.jmb2c.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lxx.jmb2c.common.PageResult;
import com.lxx.jmb2c.common.Result;
import com.lxx.jmb2c.dto.GoodsPageQueryDTO;
import com.lxx.jmb2c.entity.Goods;
import com.lxx.jmb2c.service.GoodsService;
import com.lxx.jmb2c.vo.GoodsDetailVO;
import com.lxx.jmb2c.vo.GoodsPageVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品信息表 前端控制器
 * </p>
 *
 * @author lxx
 * @since 2025-07-10
 * todo 0710 仿照member的管理后台构建商品的管理后台
 * todo 0710 仿照member的管理后台修复分类管理后台中的显示/隐藏逻辑
 */
@Slf4j
@Tag(name = "商品信息管理", description = "商品信息管理")
@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Resource
    private GoodsService goodsService;

    @Operation(summary = "添加商品信息")
    @PostMapping("")
    public Result<Object> add(@RequestBody Goods goods) {
        log.info("添加商品信息：{}", goods);
        this.goodsService.save(goods);
        return Result.success();
    }

    @Operation(summary = "修改商品信息")
    @PutMapping("")
    public Result<Object> update(@RequestBody Goods goods) {
        log.info("修改商品信息：{}", goods);
        this.goodsService.updateById(goods);
        return Result.success();
    }

    @Operation(summary = "删除商品信息")
    @DeleteMapping("")
    public Result<Object> delete(@RequestBody List<Long> ids) {
        this.goodsService.removeBatchByIds(ids);
        return Result.success();
    }

    @Operation(summary = "查询商品信息")
    @GetMapping("{id}")
    public Result<GoodsDetailVO> get(@PathVariable Long id) {
        GoodsDetailVO goodsDetailVO = this.goodsService.getGoodsDetailById(id);
        return Result.success(goodsDetailVO);
    }

    @Operation(summary = "分页查询商品信息")
    @GetMapping("page")
    public PageResult<GoodsPageVO> page(
            Long current,
            Long pageSize,
            String name,
            String goodsNo,
            Long categoryId,
            Long brandId) {
        GoodsPageQueryDTO goodsPageQueryDTO = new GoodsPageQueryDTO(
                current,
                pageSize,
                name,
                goodsNo,
                categoryId,
                brandId);
        log.info("分页查询商品信息:{}", goodsPageQueryDTO);
        IPage<GoodsPageVO> page = this.goodsService.getPage(goodsPageQueryDTO);
        return PageResult.success(page);
    }

}
